#!/usr/bin/perl
#si pas le même redpertoire mettre -I a l'appelle de perl suivi du repertoire avec les packages
use strict;
use warnings;
package p_files_with_eval_manager;

sub p_files_with_eval_manager::get_files {
	my $directoryName = $_[0];
	opendir(DIR, $directoryName) || die "Impossible d'ouvrir le dossier d'origine $directoryName\n";
	#my @files = grep { /.txt$/ } readdir(DIR);
	my @firstDirs = grep { /[^.]$/ } readdir(DIR);
	closedir DIR;

	my @files=();

	for my $firstDir (@firstDirs) {
		#printf ("$firstDir \n");
		if( $firstDir =~ /.txt$/){
			push @files, "$firstDir";
			#$nbFiles++;
		}else{
			opendir(DIR, "$directoryName/$firstDir") || die "Impossible d'ouvrir le dossier \n\t$firstDir\ndans\n\t$directoryName\n";
			my @tmpDirs = grep { /[^.]$/ } readdir(DIR);
			closedir DIR;
			for my $tmpDir (@tmpDirs) {
				push @firstDirs, "$firstDir/$tmpDir";
			}
		}
	}
	return \@files;
}

sub p_files_with_eval_manager::create_eval {
	my @files=@{get_files($_[0])};
	my %filesWithEval;
	for my $file (@files) {
        	if($file=~/star/){
                	if($file=~/0-star/){
                		$filesWithEval{$file}=[0,1,int(rand($_[1]))];
        		}elsif($file=~/1-star/){
                		$filesWithEval{$file}=[1,1,int(rand($_[1]))];
        		}elsif($file=~/2-star/){
                		$filesWithEval{$file}=[2,1,int(rand($_[1]))];
        		}elsif($file=~/3-star/){
                		$filesWithEval{$file}=[3,1,int(rand($_[1]))];
        		}
        	}else{
        		$filesWithEval{$file}=[-1,0,int(rand($_[1]))];
        	}
	}
	return \%filesWithEval;
}

sub p_files_with_eval_manager::write_eval {
	my %filesWithEval = %{$_[1]};
	open (OUT, ">./f_eval_$_[0].txt") || die $!;
	for my $file(sort { $a cmp $b } keys(%filesWithEval)){
	        print OUT "$file\t$filesWithEval{$file}[0]\t$filesWithEval{$file}[1]\t$filesWithEval{$file}[2]\n";
	}
	close OUT;
}

sub p_files_with_eval_manager::read_eval {
	my %filesWithEval = ();
	open (FIC_INDEX, "<./f_eval_$_[0].txt") || die "Impossible d'ouvrir le fichier dans read_index $_[0]\n";
	while (my $ligne = <FIC_INDEX>){
		if($ligne=~ /^(.*)\t(.*)\t(.*)\t(.*)$/){
        		$filesWithEval{$1}=[$2,$3,$4];
        	}
        }
	close OUT;
	return \%filesWithEval;
}

sub p_files_with_eval_manager::getFilesWithEval {
	my $directoryName=$_[0];
	if($directoryName!~ /[\/]$/ ){
		$directoryName="$_[0]/";
	}
	$directoryName=~/.*[\/](.*)([^\/]*)[\/]$/;
	$directoryName=$1;
	my %filesWithEval;
	my $nbPartition;
	if(!exists($_[1])){
                print "Nombre de partition mise par défaut à 10\n";
                $nbPartition=10;
        }elsif($_[1]!~/^[0-9]+$/){
                print "Nombre de partition \"$_[1]\" incorrect, quatité mise par défaut à 10\n";
                $nbPartition=10;
        }else{
                $nbPartition=$_[1];
        }
        $directoryName=$directoryName."_".$nbPartition;
	if ((!exists($_[2])||$_[2] eq 0)&&-e "./f_eval_$directoryName.txt"){
		#print"reading eval \n";
		%filesWithEval=%{&read_eval($directoryName)};
	}else{
		print"building eval\n";
		my $temps=time();
		if($_[0]=~/(.*)[\/]$/){
			%filesWithEval=%{&create_eval($1,$nbPartition)};
		}else{
			%filesWithEval=%{&create_eval($_[0],$nbPartition)};
		}
		print"writing eval\n";
		print "done in " . (time()-$temps) . " secondes\n";
		&write_eval($directoryName,\%filesWithEval);
	}
	return \%filesWithEval;
}

1;
#&getFilesWithEval($ARGV[0]);
